﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.TransportMgtDetailServiceRef;
    using WMS6.Web.TransportMgtServiceRef;

    public class Dispatch : BasePage
    {
        protected Button btnCancel;
        protected Button btnConfirm;
        protected Button btnDistribution;
        protected Button btnGoBack;
        protected Button btnSave;
        protected DropDownList ddlEquipmentType;
        protected DropDownList ddlStatus;
        protected DropDownList ddlTransportType;
        protected HtmlGenericControl divBol;
        protected HtmlForm F8021;
        protected GridView gvBol;
        protected GridView gvDeliveryOrderDetail;
        protected GridView gvEquipment;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnDeliveryOrderID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRowIndex;
        protected Label lbl;
        protected Label lblDispatchID;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblStatus;
        protected Label lblTransportType;
        protected HtmlLink mycss;
        protected RadioButtonList radbutList;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDispatchID;
        protected TextBox txtShipmentCommand;
        protected UpdatePanel up1;

        private void BindData2EquimentGridView()
        {
            try
            {
                TransportEquipmentQueryEntity transportEquipmentQuery = new TransportEquipmentQueryEntity {
                    IsGetAll = false,
                    CurrentPage = "0",
                    PageSize = this.gvEquipment.PageSize.ToString(),
                    SortField = "TE.ROWID",
                    SortDirection = "DESC"
                };
                DataSet ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(transportEquipmentQuery, "TRANSPORT_EQUIPMENT");
                base.BindData2GridView(this.gvEquipment, ds);
                this.SetTransportEquipmentQuery(transportEquipmentQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            List<string> BolDetailInfoIDs = base.GetSelectedIDsInGridView(this.gvBol, "DeliveryID");
            if ((BolDetailInfoIDs == null) || (BolDetailInfoIDs.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8200_011");
            }
            else
            {
                int i;
                List<int> IntPostion = new List<int>();
                for (i = 0; i < this.gvBol.Rows.Count; i++)
                {
                    if (((CheckBox) this.gvBol.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        IntPostion.Add(i);
                    }
                }
                List<BolDetailInfo> oldBolDetailInfos = null;
                if (this.ViewState["BolDetail"] != null)
                {
                    oldBolDetailInfos = this.ViewState["BolDetail"] as List<BolDetailInfo>;
                    if ((((oldBolDetailInfos != null) && (oldBolDetailInfos.Count > 0)) && (IntPostion != null)) && (IntPostion.Count > 0))
                    {
                        for (i = IntPostion.Count - 1; (i + 1) > 0; i--)
                        {
                            for (int n = 0; this.gvDeliveryOrderDetail.Rows.Count > n; n++)
                            {
                                if ((this.gvDeliveryOrderDetail.Rows[n].Cells[2].Text == oldBolDetailInfos[IntPostion[i]].DeliveryID) && (int.Parse(this.gvDeliveryOrderDetail.Rows[n].Cells[4].Text) == oldBolDetailInfos[IntPostion[i]].DeliveryLineID))
                                {
                                    ((Label) this.gvDeliveryOrderDetail.Rows[n].FindControl("lblResQyt")).Text = (decimal.Parse(((Label) this.gvDeliveryOrderDetail.Rows[n].FindControl("lblResQyt")).Text) + oldBolDetailInfos[IntPostion[i]].Qty).ToString();
                                    break;
                                }
                            }
                            oldBolDetailInfos.RemoveAt(IntPostion[i]);
                        }
                    }
                }
                base.BindData2GridView(this.gvBol, oldBolDetailInfos);
                this.divBol.Attributes.CssStyle.Remove("border");
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txtDispatchID.Text.Trim()))
                {
                    List<BolDetailInfo> bolDetailInfos = null;
                    bolDetailInfos = this.GetBolDetailInfoFromgvBol(true);
                    if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
                    {
                        for (int i = 0; i < bolDetailInfos.Count; i++)
                        {
                            bolDetailInfos[i].DispatchID = this.txtDispatchID.Text.Trim();
                            bolDetailInfos[i].LineID = (i + 1) * 10;
                        }
                    }
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().UpdateBolDetailList(bolDetailInfos) + base.GetGlobalMsgResource("C002");
                    this.ddlStatus.SelectedValue = "Completed";
                    this.btnConfirm.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnDistribution.Enabled = false;
                    this.btnCancel.Enabled = false;
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F8200_012");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnDistribution_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.lblMsg.Text.Trim()))
                {
                    this.lblMsg.Text = "";
                }
                List<string> DeliveryOrderIDs = base.GetSelectedIDsInGridView(this.gvDeliveryOrderDetail, "DELIVERY_ORDER_ID");
                List<string> TransportEquipmentIDs = base.GetSelectedIDsInGridView(this.gvEquipment, "TRANSPORT_EQUIPMENT_ID");
                if ((DeliveryOrderIDs == null) || (DeliveryOrderIDs.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F8200_003");
                }
                else if ((TransportEquipmentIDs == null) || (TransportEquipmentIDs.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F8200_004");
                }
                else if (TransportEquipmentIDs.Count > 1)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F8200_005");
                }
                else
                {
                    List<BolDetailInfo> bolDetailInfos = new List<BolDetailInfo>();
                    bolDetailInfos = this.GetBolDetailInfosFromgvDeliveryOrderAndgvEquipment();
                    base.BindData2GridView(this.gvBol, bolDetailInfos);
                    this.BindData2EquimentGridView();
                    this.divBol.Attributes.CssStyle.Add("border", "1px solid #c0c0c0");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("DispatchQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("DispatchList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.gvBol.Rows.Count == 0)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8200_010");
            }
            else
            {
                if (this.gvDeliveryOrderDetail.Rows.Count > 0)
                {
                    for (int i = 0; this.gvDeliveryOrderDetail.Rows.Count > i; i++)
                    {
                        string strQty = ((Label) this.gvDeliveryOrderDetail.Rows[i].FindControl("lblResQyt")).Text;
                        if (!string.IsNullOrEmpty(strQty) && (decimal.Parse(strQty) > 0M))
                        {
                            this.lblMsg.Text = "请将运单货品全部分配!";
                            return;
                        }
                    }
                }
                if (!string.IsNullOrEmpty(this.txtDispatchID.Text.Trim()))
                {
                    this.Update();
                }
                else
                {
                    this.Insert();
                }
            }
        }

        private List<BolDetailInfo> changeDs2BolDetailInfo(DataSet ds)
        {
            decimal tempDec = 0.0M;
            List<BolDetailInfo> bolDetailInfos = null;
            if ((ds != null) && (ds.Tables[2].Rows.Count > 0))
            {
                bolDetailInfos = new List<BolDetailInfo>();
                for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                {
                    BolDetailInfo bolDetailInfo = new BolDetailInfo {
                        BolID = (ds.Tables[2].Rows[i]["BOL_ID"] != null) ? ds.Tables[2].Rows[i]["BOL_ID"].ToString() : "",
                        ActualEquipment = (ds.Tables[2].Rows[i]["ACTUAL_EQUIPMENT"] != null) ? ds.Tables[2].Rows[i]["ACTUAL_EQUIPMENT"].ToString() : "",
                        ActualEquipmentType = (ds.Tables[2].Rows[i]["ACTUAL_EQUIPMENT_TYPE"] != null) ? ds.Tables[2].Rows[i]["ACTUAL_EQUIPMENT_TYPE"].ToString() : "",
                        Driver = (ds.Tables[2].Rows[i]["DRIVER"] != null) ? ds.Tables[2].Rows[i]["DRIVER"].ToString() : "",
                        DispatchedBy = (ds.Tables[2].Rows[i]["DISPATCHED_BY"] != null) ? ds.Tables[2].Rows[i]["DISPATCHED_BY"].ToString() : "",
                        PlanEquipment = (ds.Tables[2].Rows[i]["PLAN_EQUIPMENT"] != null) ? ds.Tables[2].Rows[i]["PLAN_EQUIPMENT"].ToString() : "",
                        PlanEquipmentType = (ds.Tables[2].Rows[i]["PLAN_EQUIPMENT_TYPE"] != null) ? ds.Tables[2].Rows[i]["PLAN_EQUIPMENT_TYPE"].ToString() : "",
                        Carrier = (ds.Tables[2].Rows[i]["CARRIER"] != null) ? ds.Tables[2].Rows[i]["CARRIER"].ToString() : "",
                        SkuID = (ds.Tables[2].Rows[i]["SKU_ID"] != null) ? ds.Tables[2].Rows[i]["SKU_ID"].ToString() : "",
                        OwnerID = (ds.Tables[2].Rows[i]["OWNER_ID"] != null) ? ds.Tables[2].Rows[i]["OWNER_ID"].ToString() : "",
                        DeliveryID = (ds.Tables[2].Rows[i]["DELIVERY_ID"] != null) ? ds.Tables[2].Rows[i]["DELIVERY_ID"].ToString() : "",
                        DeliveryLineID = (ds.Tables[2].Rows[i]["DELIVERY_LINE_ID"] != null) ? Convert.ToInt32(ds.Tables[2].Rows[i]["DELIVERY_LINE_ID"]) : 0,
                        LineID = int.Parse(ds.Tables[2].Rows[i]["LINE_ID"].ToString()),
                        Qty = decimal.TryParse(ds.Tables[2].Rows[i]["QTY"].ToString(), out tempDec) ? decimal.Parse(ds.Tables[2].Rows[i]["QTY"].ToString()) : tempDec,
                        PackID = (ds.Tables[2].Rows[i]["PACK_ID"] != null) ? ds.Tables[2].Rows[i]["PACK_ID"].ToString() : "",
                        NetWeight = decimal.TryParse(ds.Tables[2].Rows[i]["NET_WEIGHT"].ToString(), out tempDec) ? decimal.Parse(ds.Tables[2].Rows[i]["NET_WEIGHT"].ToString()) : tempDec,
                        Cube = decimal.TryParse(ds.Tables[2].Rows[i]["CUBE"].ToString(), out tempDec) ? decimal.Parse(ds.Tables[2].Rows[i]["CUBE"].ToString()) : tempDec,
                        TransportType = (ds.Tables[2].Rows[i]["TRANSPORT_TYPE"] != null) ? ds.Tables[2].Rows[i]["TRANSPORT_TYPE"].ToString() : "",
                        TransportMode = (ds.Tables[2].Rows[i]["TRANSPORT_MODE"] != null) ? ds.Tables[2].Rows[i]["TRANSPORT_MODE"].ToString() : ""
                    };
                    bolDetailInfos.Add(bolDetailInfo);
                }
            }
            return bolDetailInfos;
        }

        private List<BolDetailInfo> GetBolDetailInfoFromgvBol(bool flage)
        {
            decimal tempDec = 0.0M;
            List<BolDetailInfo> bolDetailInfos = new List<BolDetailInfo>();
            for (int i = 0; i < this.gvBol.Rows.Count; i++)
            {
                if (flage)
                {
                    int CS00002;
                    if (string.IsNullOrEmpty(((TextBox) this.gvBol.Rows[i].FindControl("txtDriver")).Text))
                    {
                        CS00002 = i + 1;
                        throw new Exception(string.Format(base.GetGlobalMsgResource("F8200_013"), CS00002.ToString()));
                    }
                    if (string.IsNullOrEmpty(((TextBox) this.gvBol.Rows[i].FindControl("txtActualEquipment")).Text))
                    {
                        CS00002 = i + 1;
                        throw new Exception(string.Format(base.GetGlobalMsgResource("F8200_014"), CS00002.ToString()));
                    }
                    if (string.IsNullOrEmpty(((TextBox) this.gvBol.Rows[i].FindControl("txtActualEquipmentType")).Text))
                    {
                        CS00002 = i + 1;
                        throw new Exception(string.Format(base.GetGlobalMsgResource("F8200_015"), CS00002.ToString()));
                    }
                }
                BolDetailInfo bolDetailInfo = new BolDetailInfo {
                    BolID = (this.gvBol.DataKeys[i]["BOLID"] != null) ? this.gvBol.DataKeys[i]["BOLID"].ToString() : string.Empty,
                    PlanEquipment = this.gvBol.DataKeys[i]["PlanEquipment"].ToString(),
                    PlanEquipmentType = this.gvBol.DataKeys[i]["PlanEquipmentType"].ToString(),
                    ActualEquipment = ((TextBox) this.gvBol.Rows[i].FindControl("txtActualEquipment")).Text,
                    ActualEquipmentType = ((TextBox) this.gvBol.Rows[i].FindControl("txtActualEquipmentType")).Text,
                    OwnerID = this.gvBol.DataKeys[i]["OWNERID"].ToString(),
                    SkuID = this.gvBol.DataKeys[i]["SkuID"].ToString(),
                    Driver = ((TextBox) this.gvBol.Rows[i].FindControl("txtDriver")).Text,
                    Carrier = ((Label) this.gvBol.Rows[i].FindControl("lblCarrierID")).Text,
                    DispatchedBy = ((TextBox) this.gvBol.Rows[i].FindControl("txtDispatchedBy")).Text,
                    DeliveryID = this.gvBol.DataKeys[i]["DeliveryID"].ToString(),
                    DeliveryLineID = int.Parse(this.gvBol.DataKeys[i]["DeliveryLineID"].ToString()),
                    Qty = decimal.TryParse(this.gvBol.DataKeys[i]["QTY"].ToString(), out tempDec) ? decimal.Parse(this.gvBol.DataKeys[i]["QTY"].ToString()) : tempDec,
                    PackID = this.gvBol.DataKeys[i]["PackID"].ToString(),
                    GrossWeight = decimal.TryParse(this.gvBol.DataKeys[i]["GrossWeight"].ToString(), out tempDec) ? decimal.Parse(this.gvBol.DataKeys[i]["GrossWeight"].ToString()) : tempDec,
                    Cube = decimal.TryParse(this.gvBol.DataKeys[i]["Cube"].ToString(), out tempDec) ? decimal.Parse(this.gvBol.DataKeys[i]["Cube"].ToString()) : tempDec,
                    TransportMode = this.gvBol.DataKeys[i]["TransportMode"].ToString(),
                    TransportType = this.gvBol.DataKeys[i]["TransportType"].ToString(),
                    CreatedBy = base.UserInfo.CreatedBy,
                    UpdatedBy = base.UserInfo.CreatedBy,
                    IsActive = "Y"
                };
                bolDetailInfos.Add(bolDetailInfo);
            }
            return bolDetailInfos;
        }

        private List<BolDetailInfo> GetBolDetailInfosFromgvDeliveryOrderAndgvEquipment()
        {
            int i;
            TransportEquipmentInfo transportEquipmentInfo = new TransportEquipmentInfo();
            for (i = 0; i < this.gvEquipment.Rows.Count; i++)
            {
                if (((CheckBox) this.gvEquipment.Rows[i].FindControl("chbSelect")).Checked)
                {
                    transportEquipmentInfo.TransportEquipmentID = this.gvEquipment.DataKeys[i]["TRANSPORT_EQUIPMENT_ID"].ToString();
                    transportEquipmentInfo.EquipmentTypeID = this.gvEquipment.DataKeys[i]["EQUIPMENT_TYPE_ID"].ToString();
                    transportEquipmentInfo.CarrierID = this.gvEquipment.DataKeys[i]["CARRIER_ID"].ToString();
                }
            }
            List<DeliveryOrderAndDispatchDataInfo> deliveryOrderAndDispatchDataInfos = new List<DeliveryOrderAndDispatchDataInfo>();
            for (i = 0; i < this.gvDeliveryOrderDetail.Rows.Count; i++)
            {
                if (((CheckBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("chbSelect")).Checked)
                {
                    if (decimal.Parse(((Label) this.gvDeliveryOrderDetail.Rows[i].FindControl("lblResQyt")).Text.Trim()) < 0.0M)
                    {
                        this.lblMsg.Text = this.gvDeliveryOrderDetail.DataKeys[i]["LINE_ID"].ToString() + base.GetGlobalMsgResource("F8200_007");
                        ((TextBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("txtLoadingQty")).Text = base.GetPreferenceDigit((decimal) 0.0M);
                        return null;
                    }
                    if ((decimal.Parse(((Label) this.gvDeliveryOrderDetail.Rows[i].FindControl("lblResQyt")).Text) - decimal.Parse(((TextBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("txtLoadingQty")).Text)) < 0.0M)
                    {
                        this.lblMsg.Text = this.gvDeliveryOrderDetail.DataKeys[i]["LINE_ID"].ToString() + base.GetGlobalMsgResource("F8200_008");
                        ((TextBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("txtLoadingQty")).Text = base.GetPreferenceDigit((decimal) 0.0M);
                        return null;
                    }
                    if (((Label) this.gvDeliveryOrderDetail.Rows[i].FindControl("lblResQyt")).Text.Trim() != base.GetPreferenceDigit("0"))
                    {
                        DeliveryOrderAndDispatchDataInfo deliveryOrderAndDispatchDataInfo = new DeliveryOrderAndDispatchDataInfo {
                            DELIVERY_ORDER_ID = this.gvDeliveryOrderDetail.DataKeys[i]["DELIVERY_ORDER_ID"].ToString(),
                            LINE_ID = this.gvDeliveryOrderDetail.DataKeys[i]["LINE_ID"].ToString(),
                            OWNER_ID = this.gvDeliveryOrderDetail.DataKeys[i]["OWNER_ID"].ToString(),
                            LOADING_QTY = decimal.Parse(((TextBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("txtLoadingQty")).Text),
                            PACK_ID = this.gvDeliveryOrderDetail.DataKeys[i]["PACK_ID"].ToString()
                        };
                        ((Label) this.gvDeliveryOrderDetail.Rows[i].FindControl("lblResQyt")).Text = base.GetPreferenceDigit((decimal.Parse(((Label) this.gvDeliveryOrderDetail.Rows[i].FindControl("lblResQyt")).Text) - decimal.Parse(((TextBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("txtLoadingQty")).Text)).ToString());
                        deliveryOrderAndDispatchDataInfo.SKU_ID = this.gvDeliveryOrderDetail.DataKeys[i]["SKU_ID"].ToString();
                        deliveryOrderAndDispatchDataInfo.TOTAL_WEIGHT = decimal.Parse(this.gvDeliveryOrderDetail.DataKeys[i]["TOTAL_WEIGHT"].ToString());
                        deliveryOrderAndDispatchDataInfo.TRANSFER_TYPE = this.gvDeliveryOrderDetail.DataKeys[i]["TRANSFER_TYPE"].ToString();
                        deliveryOrderAndDispatchDataInfo.DELIVERY_ORDER_TYPE = this.gvDeliveryOrderDetail.DataKeys[i]["DELIVERY_ORDER_TYPE"].ToString();
                        deliveryOrderAndDispatchDataInfos.Add(deliveryOrderAndDispatchDataInfo);
                        goto Label_0556;
                    }
                    this.lblMsg.Text = this.gvDeliveryOrderDetail.DataKeys[i]["LINE_ID"].ToString() + base.GetGlobalMsgResource("F8200_009");
                }
                continue;
            Label_0556:
                ((TextBox) this.gvDeliveryOrderDetail.Rows[i].FindControl("txtLoadingQty")).Text = base.GetPreferenceDigit((decimal) 0.0M);
            }
            List<BolDetailInfo> bolDetailInfos = null;
            if ((deliveryOrderAndDispatchDataInfos != null) && (deliveryOrderAndDispatchDataInfos.Count > 0))
            {
                if (this.ViewState["BolDetail"] != null)
                {
                    bolDetailInfos = this.ViewState["BolDetail"] as List<BolDetailInfo>;
                }
                else
                {
                    bolDetailInfos = new List<BolDetailInfo>();
                }
                for (i = 0; i < deliveryOrderAndDispatchDataInfos.Count; i++)
                {
                    BolDetailInfo bolDetailInfo = new BolDetailInfo();
                    bolDetailInfo.BolID = "";
                    bolDetailInfo.ActualEquipment = "";
                    bolDetailInfo.ActualEquipmentType = "";
                    bolDetailInfo.Driver = "";
                    bolDetailInfo.DispatchedBy = base.UserInfo.UserName;
                    bolDetailInfo.PlanEquipment = transportEquipmentInfo.TransportEquipmentID;
                    bolDetailInfo.PlanEquipmentType = transportEquipmentInfo.EquipmentTypeID;
                    bolDetailInfo.ActualEquipment = transportEquipmentInfo.TransportEquipmentID;
                    bolDetailInfo.ActualEquipmentType = transportEquipmentInfo.EquipmentTypeID;
                    bolDetailInfo.Carrier = transportEquipmentInfo.CarrierID;
                    bolDetailInfo.SkuID = deliveryOrderAndDispatchDataInfos[i].SKU_ID;
                    bolDetailInfo.OwnerID = deliveryOrderAndDispatchDataInfos[i].OWNER_ID;
                    bolDetailInfo.DeliveryID = deliveryOrderAndDispatchDataInfos[i].DELIVERY_ORDER_ID;
                    bolDetailInfo.DeliveryLineID = int.Parse(deliveryOrderAndDispatchDataInfos[i].LINE_ID);
                    bolDetailInfo.LineID = int.Parse(deliveryOrderAndDispatchDataInfos[i].LINE_ID);
                    bolDetailInfo.Qty = deliveryOrderAndDispatchDataInfos[i].LOADING_QTY;
                    bolDetailInfo.PackID = deliveryOrderAndDispatchDataInfos[i].PACK_ID;
                    bolDetailInfo.NetWeight = deliveryOrderAndDispatchDataInfos[i].TOTAL_WEIGHT;
                    bolDetailInfo.Cube = deliveryOrderAndDispatchDataInfos[i].TOTAL_CUBE;
                    bolDetailInfo.TransportType = deliveryOrderAndDispatchDataInfos[i].TRANSFER_TYPE;
                    bolDetailInfo.TransportMode = deliveryOrderAndDispatchDataInfos[i].TRANSFER_TYPE;
                    if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
                    {
                        if (!bolDetailInfos.Contains(bolDetailInfo))
                        {
                            bolDetailInfos.Add(bolDetailInfo);
                        }
                    }
                    else
                    {
                        bolDetailInfos.Add(bolDetailInfo);
                    }
                }
            }
            this.ViewState["BolDetail"] = bolDetailInfos;
            return bolDetailInfos;
        }

        private TransportEquipmentQueryEntity GetTransportEquipmentQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as TransportEquipmentQueryEntity);
            }
            return (base.PageQueryEntitySession as TransportEquipmentQueryEntity);
        }

        protected void gvBol_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvBol.PageIndex * this.gvBol.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                ((TextBox) e.Row.FindControl("txtActualEquipment")).Attributes.Add("onblur", "setActualEquipmentRowIndex(" + e.Row.RowIndex + ")");
                ((Image) e.Row.FindControl("imgSelectActualEquipment")).Attributes.Add("onclick", "SelectActualEquipment(" + ((TextBox) e.Row.FindControl("txtActualEquipment")).ClientID + ",'" + ((Label) e.Row.FindControl("lblCarrierID")).Text + "')");
                ((Image) e.Row.FindControl("imgSelectActualEquipmentType")).Attributes.Add("onclick", "SelectActualEquipmentType(" + ((TextBox) e.Row.FindControl("txtActualEquipmentType")).ClientID + ",'" + ((Label) e.Row.FindControl("lblCarrierID")).Text + "')");
                ((Image) e.Row.FindControl("imgSelectDriver")).Attributes.Add("onclick", "SelectDriver(" + ((TextBox) e.Row.FindControl("txtDriver")).ClientID + ",'" + ((Label) e.Row.FindControl("lblCarrierID")).Text + "')");
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvDeliveryOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvDeliveryOrderDetail.PageIndex * this.gvDeliveryOrderDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                ((TextBox) e.Row.FindControl("txtLoadingQty")).Attributes.Add("onchange", "calculatQty(" + ((Label) e.Row.FindControl("lblResQyt")).ClientID + "," + ((TextBox) e.Row.FindControl("txtLoadingQty")).ClientID + "," + ((Label) e.Row.FindControl("lblResQyt")).Text + ")");
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvEquipment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvEquipment.PageIndex * this.gvEquipment.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvEquipment_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                TransportEquipmentQueryEntity transportEquipmentQuery = this.GetTransportEquipmentQueryEntity();
                transportEquipmentQuery.SortField = e.SortExpression;
                if (transportEquipmentQuery.SortDirection == "ASC")
                {
                    transportEquipmentQuery.SortDirection = "DESC";
                }
                else if (transportEquipmentQuery.SortDirection == "DESC")
                {
                    transportEquipmentQuery.SortDirection = "ASC";
                }
                DataSet ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(transportEquipmentQuery, "TRANSPORT_EQUIPMENT");
                base.BindData2GridView(this.gvEquipment, ds);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void Insert()
        {
            try
            {
                DispatchInfo dispatchInfo = new DispatchInfo {
                    DispatchDetailInfos = new List<WMS6.Web.TransportMgtServiceRef.DispatchDetailInfo>(),
                    Status = "New",
                    PickupDate = DateTime.Now,
                    DeliveryDate = DateTime.Now,
                    PlanTransportMode = this.ddlTransportType.SelectedValue,
                    ShipmentCommand = this.txtShipmentCommand.Text.Trim(),
                    IsActive = "Y",
                    CreatedBy = base.UserInfo.UserID
                };
                string temp = this.hdnDeliveryOrderID.Value;
                dispatchInfo.DispatchDetailInfos = new List<WMS6.Web.TransportMgtServiceRef.DispatchDetailInfo>();
                List<string> deliveryOrderIDS = temp.Split(new char[] { ',' }).ToList<string>();
                if ((deliveryOrderIDS != null) && (deliveryOrderIDS.Count > 0))
                {
                    for (int i = 0; i < deliveryOrderIDS.Count; i++)
                    {
                        WMS6.Web.TransportMgtServiceRef.DispatchDetailInfo dispatchDetailInfo = new WMS6.Web.TransportMgtServiceRef.DispatchDetailInfo {
                            DeliveryOrderID = deliveryOrderIDS[i],
                            IsActive = "Y",
                            CreatedBy = base.UserInfo.CreatedBy,
                            LineID = (i + 1) * 10
                        };
                        dispatchInfo.DispatchDetailInfos.Add(dispatchDetailInfo);
                    }
                }
                List<BolDetailInfo> bolDetailInfos = this.GetBolDetailInfoFromgvBol(true);
                this.txtDispatchID.Text = TransportMgtServiceClientProxy.GetInstance().InsertDispatchAndBolDetailList(dispatchInfo, bolDetailInfos);
                this.lblMsg.Text = this.txtDispatchID.Text + base.GetGlobalMsgResource("C004");
                BolDetailQueryEntity bolDetailQuery = new BolDetailQueryEntity {
                    DispatchID = "= " + this.txtDispatchID.Text.Trim(),
                    IsGetAll = false,
                    CurrentPage = "0",
                    PageSize = this.gvBol.PageSize.ToString(),
                    SortField = "ROWID",
                    SortDirection = "ASC"
                };
                DataSet ds = TransportMgtServiceClientProxy.GetInstance().GetBolDetailByQueryEntity(bolDetailQuery);
                bolDetailInfos = null;
                bolDetailInfos = this.changeDs2BolDetailInfo(ds);
                if (bolDetailInfos != null)
                {
                    base.BindData2GridView(this.gvBol, bolDetailInfos);
                    if (this.ViewState["BolDetail"] != null)
                    {
                        this.ViewState["BolDetail"] = null;
                    }
                }
                this.txtDispatchID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlTransportType, "TransportMode");
            if (base.Request.QueryString["ID"] != null)
            {
                this.hdnDeliveryOrderID.Value = base.Request.QueryString["ID"].ToString();
                this.SetDispatchInfo2Form();
            }
            if (base.Request.QueryString["DID"] != null)
            {
                this.SetDispatchInfoByDisPatchID2Form();
            }
            if (base.Request.QueryString["Status"] != null)
            {
                if (base.Request.QueryString["Status"].ToString() == "Completed")
                {
                    this.ddlStatus.SelectedValue = base.Request.QueryString["Status"].ToString();
                    this.btnSave.Enabled = false;
                    this.btnConfirm.Enabled = false;
                    this.btnDistribution.Enabled = false;
                    this.btnCancel.Enabled = false;
                }
            }
            else
            {
                this.ddlStatus.SelectedValue = "New";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
                this.divBol.Attributes.CssStyle.Remove("border");
            }
            this.lblMsg.Text = "";
        }

        private void SetDispatchInfo2Form()
        {
            try
            {
                string[] temps = this.hdnDeliveryOrderID.Value.Split(new char[] { ',' });
                List<string> deliveryOrderIDS = new List<string>();
                if (temps.Length > 0)
                {
                    for (int i = 0; i < temps.Length; i++)
                    {
                        deliveryOrderIDS.Add(temps[i].ToString());
                    }
                }
                List<DeliveryOrderAndDispatchDataInfo> deliveryOrderAndDispatchDataInfos = TransportDetailMgtServiceClientProxy.GetInstance().GetDeliveryOrderAndDispatchDataInfoByIDs(deliveryOrderIDS);
                base.BindData2GridView(this.gvDeliveryOrderDetail, deliveryOrderAndDispatchDataInfos);
                this.BindData2EquimentGridView();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetDispatchInfoByDisPatchID2Form()
        {
            string tempDispatchID = base.Request.QueryString["DID"].ToString();
            this.txtDispatchID.Text = tempDispatchID;
            this.txtDispatchID.Enabled = false;
            DispatchInfo dispatchInfo = TransportMgtServiceClientProxy.GetInstance().GetDispatchInfoByID(tempDispatchID);
            if (dispatchInfo != null)
            {
                this.ddlTransportType.SelectedValue = dispatchInfo.PlanTransportMode;
                this.txtShipmentCommand.Text = dispatchInfo.ShipmentCommand;
            }
            List<string> deliveryOrderIDs = new List<string>();
            List<WMS6.Web.TransportMgtDetailServiceRef.DispatchDetailInfo> dispatchDetailInfos = TransportDetailMgtServiceClientProxy.GetInstance().GetDispatchDetailInfoListByID(tempDispatchID);
            if ((dispatchDetailInfos != null) && (dispatchDetailInfos.Count > 0))
            {
                int i;
                for (i = 0; i < dispatchDetailInfos.Count; i++)
                {
                    if (!deliveryOrderIDs.Contains(dispatchDetailInfos[i].DeliveryOrderID))
                    {
                        deliveryOrderIDs.Add(dispatchDetailInfos[i].DeliveryOrderID);
                    }
                }
                string temp = string.Empty;
                if ((deliveryOrderIDs != null) && (deliveryOrderIDs.Count > 0))
                {
                    for (i = 0; i < deliveryOrderIDs.Count; i++)
                    {
                        temp = temp + deliveryOrderIDs[i] + ",";
                    }
                }
                if (!(string.IsNullOrEmpty(temp) || (temp.Length <= 0)))
                {
                    temp = temp.Substring(0, temp.Length - 1);
                }
                this.hdnDeliveryOrderID.Value = temp;
                List<DeliveryOrderAndDispatchDataInfo> deliveryOrderAndDispatchDataInfos = TransportDetailMgtServiceClientProxy.GetInstance().GetDeliveryOrderAndDispatchDataInfoByIDs(deliveryOrderIDs);
                base.BindData2GridView(this.gvDeliveryOrderDetail, deliveryOrderAndDispatchDataInfos);
            }
            this.BindData2EquimentGridView();
            BolDetailQueryEntity bolDetailQuery = new BolDetailQueryEntity {
                DispatchID = "= " + tempDispatchID,
                IsGetAll = false,
                CurrentPage = "0",
                PageSize = this.gvBol.PageSize.ToString(),
                SortField = "ROWID",
                SortDirection = "ASC"
            };
            DataSet ds = TransportMgtServiceClientProxy.GetInstance().GetBolDetailByQueryEntity(bolDetailQuery);
            List<BolDetailInfo> bolDetailInfos = null;
            bolDetailInfos = this.changeDs2BolDetailInfo(ds);
            if (bolDetailInfos != null)
            {
                base.BindData2GridView(this.gvBol, bolDetailInfos);
                if (this.ViewState["BolDetail"] == null)
                {
                    this.ViewState["BolDetail"] = bolDetailInfos;
                }
            }
        }

        private void SetTransportEquipmentQuery(TransportEquipmentQueryEntity transportEquipmentQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = transportEquipmentQuery;
            }
            else
            {
                base.PageQueryEntitySession = transportEquipmentQuery;
            }
        }

        protected void txtActualEquipment_TextChanged(object sender, EventArgs e)
        {
            int rowIndex = 0;
            if (!string.IsNullOrEmpty(this.hdnRowIndex.Value))
            {
                rowIndex = int.Parse(this.hdnRowIndex.Value);
                string actualEquipment = ((TextBox) this.gvBol.Rows[rowIndex].FindControl("txtActualEquipment")).Text.Trim();
                TransportEquipmentInfo transportEquipmentInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(actualEquipment, "TRANSPORT_EQUIPMENT") as TransportEquipmentInfo;
                if (transportEquipmentInfo != null)
                {
                    if ((transportEquipmentInfo.Status == "Free") || (transportEquipmentInfo.Status == "Loading"))
                    {
                        ((TextBox) this.gvBol.Rows[rowIndex].FindControl("txtActualEquipmentType")).Text = transportEquipmentInfo.EquipmentTypeID;
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F8200_006");
                    }
                }
            }
        }

        protected void txtDriver_Change(object sender, EventArgs e)
        {
            List<BolDetailInfo> bolDetailInfos = this.GetBolDetailInfoFromgvBol(false);
            if ((bolDetailInfos != null) && (bolDetailInfos.Count > 0))
            {
                string temp = string.Empty;
                for (int i = 0; i < bolDetailInfos.Count; i++)
                {
                    if (bolDetailInfos[i].Driver == ((TextBox) sender).Text)
                    {
                        temp = bolDetailInfos[i].ActualEquipment;
                    }
                }
                for (int j = 0; j < bolDetailInfos.Count; j++)
                {
                    if (!string.IsNullOrEmpty(temp) && (bolDetailInfos[j].ActualEquipment == temp))
                    {
                        bolDetailInfos[j].Driver = ((TextBox) sender).Text;
                    }
                }
            }
            base.BindData2GridView(this.gvBol, bolDetailInfos);
            if (this.ViewState["BolDetail"] == null)
            {
                this.ViewState["BolDetail"] = bolDetailInfos;
            }
        }

        private void Update()
        {
            try
            {
                List<string> dispatchIDs = new List<string> {
                    this.txtDispatchID.Text.Trim()
                };
                TransportMgtServiceClientProxy.GetInstance().DeleteDispatchInfo(dispatchIDs);
                this.Insert();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

